<h3 class="page-header">Thêm tự động đang được hoàn tất</h3>
<div class="row">
    <div class="col-md-6">
        đang chạy dữ liệu
    </div>
</div>
<?php
    if($_POST['submit']){
        $webinput   = $_POST['webinput'];
        $link       = $_POST['link'];
        if($webinput && $link){
            if($webinput == 1){
                // conecting database
                conectingDatabase();
                // load library simple html dom
                require_once "simplehtmldom/simple_html_dom.php";
                $news = array();
                $url = $link;
                $html = file_get_html($url);
                foreach($html->find('.item-section li .yt-lockup .yt-lockup-content h3.yt-lockup-title a.yt-uix-tile-link') as $title){
                    $news[]['title'] = $title->plaintext;
                }
                $count = 0;
                foreach($html->find('.item-section li .yt-lockup .yt-lockup-content h3.yt-lockup-title a.yt-uix-tile-link') as $title){    
                    preg_match_all("#(?<=v=|v\/|vi=|vi\/|youtu.be\/)[a-zA-Z0-9_-]{11}#", $title->href, $matches);
                    $news[$count]['href'] = $matches[0][0];
                    $count ++;
                }
                $count = 0;
                foreach($html->find('div.yt-lockup-thumbnail div.yt-thumb img') as $title){
                    $news[$count]['src'] = $title->src;
                    $count ++;
                } 
                // insert database
                foreach($news as $new){
                    $link_seo = seo_url($new['title']);
                    $sql  = "INSERT INTO app_ty (idcat,name, url, code, title, photo)";
                    $sql .= " VALUES (5,'".$new['title']."','".$link_seo."','".$new['href']."','".$new['title']."','".$new['src']."')";
                    mysql_query($sql);
                }                 
                // close conecting
                closeDB();
            }
        }
    }
?>